<?php
namespace App\Http\Service;

use Illuminate\Http\Request;
use App\Http\Service\CommentService;
use App\Models\Role;
use App\Models\RoleNode;
use App\Models\Node;
use App\Models\AdminRole;

class RoleService
{
	private static $_obj;
	private $_comment;
	private $_roleModel;
	private $_nodeModel;
	private $_roleNode;

	public static function getInstance()
	{
		if (!self::$_obj instanceof self) {
			self::$_obj = new self();
		}
		return self::$_obj;
	}

	public function __construct()
	{
		$this->_comment = CommentService::getInstance();
		$this->_roleModel = Role::getInstance();
		$this->_nodeModel = Node::getInstance();
		$this->_roleNode = RoleNode::getInstance();
	}

	/**
	 * 获取角色和角色权限
	 * @param int $id 角色id
	 */
	public function getRoleAndRoleNode($id)
	{
		$data = $this->_roleModel->AddToRole($id);
		$data['rolenode'] = $this->roleNode->getNodeData($id);
		return $data;
	}

	/**
	 * 数据库添加操作
	 * @param Object $data 页面传输过来的值
	 */
	public function addToRole(Request $data)
	{

		$rid = $this->_roleModel->addToRole($data);
		if ($rid) {
			if ($data->nid && is_array($data->nid)) {
				$res = $this->_roleNode->addRoleNode($data->nid, $rid);
				if ($res) {
					return $this->_comment->returnArr(200, '添加成功');
				} else {
					return $this->_comment->returnArr(300, '权限添加失败');
				}
			}
		} else {
			return $this->_comment->returnArr(300, '添加失败');
		}
	}

	/**
	 * 数据库编辑操作
	 * @param Object $data 页面传输过来的数据
	 * @param int $id 角色id
	 */
	public function editRole(Request $data, $id)
	{
		$result = $this->_roleModel->editRole($data, $id);
		if ($result) {
			if ($data->status == 2) {
				AdminRole::getInstance()->delAdminRoleByRid($id);  //编辑状态和删除时,更新管理员角色关联表
			}
			$this->roleNode->delRoleNodeByRid($id);  //不管成功与否,先删除掉关联表
			if ($data->nid && is_array($data->nid)) {
				$res = $this->roleNode->addRoleNode($data->nid, $id);
				if ($res) {
					return $this->_comment->returnArr(200, '编辑成功');
				} else {
					return $this->_comment->returnArr(200, '节点权限未添加');
				}
			}
			return $this->_comment->returnArr(200, '编辑成功');
		} else {
			return $this->_comment->returnArr(300, '编辑失败');
		}
	}

	/**
	 * 数据库删除操作
	 */
	public function delete($id)
	{
		$result = $this->_role->delRole($id);
		if ($result) {
			$res = $this->_roleNode->delRoleNodeByRid($id);
			if ($res) {
				AdminRole::getInstance()->delAdminRoleByRid($id);  //编辑状态和删除时,更新管理员角色关联表
				return $this->_comment->returnArr(200, '删除成功');
			} else {
				return $this->_comment->returnArr(200, '删除成功,关联表删除失败');
			}
		} else {
			return $this->_comment->returnArr(300, '删除失败');
		}
	}

	/**
	 * 数据库状态修改
	 */
	public function status($id)
	{
		$result = $this->_role->statusRole($id);
		if ($result) {
			return $this->_comment->returnArr(200, '更改成功');
		} else {
			return $this->_comment->returnArr(300, '更改失败');
		}
	}

	/**
	 * 获取分组权限列表
	 */
	public function getRoleNode()
	{
		$node = $this->_nodeModel->getControllerNameByGroup();
		if (!$node) {
			return $this->_comment->returnArr(300, '控制器名称查询失败');
		}
		$groupNode = $this->_nodeModel->getGroupByActionNameByControllerName($node);
		if ($groupNode) {
			return $this->_comment->returnArr(200, '查询成功', $groupNode);
		} else {
			return $this->_comment->returnArr(300, '查询失败');
		}
	}
}